﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Sources;

namespace console.demo.senior
{
    public class EventDemo
    {
        private int value;

        public delegate void NumManipulaionHandle();

        public event NumManipulaionHandle ChangeNum;

        protected virtual void OnNumChanged()
        {
            if (ChangeNum != null)
            {
                ChangeNum();
            }
            else
            {
                Console.WriteLine("event not fire");
                Console.ReadKey();
            }
        }

        public EventDemo()
        {
            int n = 5;
            setValue(n);
        }
        public void setValue(int n)
        {
            if (value != n)
            {
                value = n;
                OnNumChanged();
            }
        }

        /**
         * 订阅器类
         */
        public class subscribeEvent
        {
            public void printF()
            {
                Console.WriteLine("event fire");
                Console.ReadKey();
            }
        }

        public void get()
        {
            EventDemo eventDemo = new EventDemo();
            subscribeEvent subscribeEvent = new subscribeEvent();
            eventDemo.ChangeNum += new EventDemo.NumManipulaionHandle(subscribeEvent.printF);
            eventDemo.setValue(7);
            eventDemo.setValue(12);
        }
    }
}
